package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.Raw;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.Supplier;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.LabelValueVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.RawMapper;
import com.trace.app.service.RawService;
import com.trace.app.service.SupplierService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Raw)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-21 13:27:18
 */
@Service("rawService")
public class RawServiceImpl extends ServiceImpl<RawMapper, Raw> implements RawService {

    @Resource
    private SupplierService supplierService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Raw> queryWrapper = new LambdaQueryWrapper<>();
        Page<Raw> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Raw> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Raw raw) {
        return save(raw) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Raw raw) {
        LambdaUpdateWrapper<Raw> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Raw::getId, raw.getId());
        if (raw.getName() != null) {
            updateWrapper.set(Raw::getName, raw.getName());
        }
        if (raw.getSupId() != null) {
            updateWrapper.set(Raw::getSupId, raw.getSupId());
        }
        if (raw.getDateTime() != null) {
            updateWrapper.set(Raw::getDateTime, raw.getDateTime());
        }
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Raw> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Raw::getName, name);
        Page<Raw> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Raw> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getLabelValue() {
        List<LabelValueVo> collect = list().stream().map((raw -> {
            LabelValueVo labelValueVo = new LabelValueVo();
            labelValueVo.setValue(String.valueOf(raw.getId()));
            labelValueVo.setLabel(raw.getName());
            return labelValueVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult getSupplierByRawId(Integer rawId) {
        Raw raw = getById(rawId);
        Supplier supplier = supplierService.getById(raw.getSupId());

        return ResponseResult.okResult(supplier);
    }
}

